<?php
namespace Admin\Controller;
use Admin\Controller\BaseController;
class VideoController extends BaseController {

    //视频列表
    public function index(){
        $data = array_filter($_GET);
        if (!empty($_GET['video_name'])) {
            $data['video_name'] = array('like',"%".trim($_GET['video_name'])."%");
        }
        unset($data['p']);
        $data['video_display'] = 1;
        $count = M('video')->where($data)->count();
        $page = getpage($count,10);
        $video_arr = M('video')->where($data)->order('video_order desc,video_id desc')->limit($page->firstRow, $page->listRows)->select();
        $this->assign('video_arr', $video_arr);
        $this->assign('page', $page->show());
        $this->display();
    }

    //添加视频
    public function video_add(){
        if (I('post.action') == 'video_add_action') {
            $this->video_add_action();
            exit();
        }
        $this->display();
    }

    //添加视频处理
    public function video_add_action(){
        if($_POST != array_filter($_POST)){
            $this->error('视频选项不能有空值！');
        }
        //图片文件上传
        // $finfoicon = upload($_FILES['video_icon']);
        $finfopic = upload($_FILES['video_pic']);
        // //如果有一个上传失败，则把成功的文件也删除掉
        // if ($finfoicon['status'] == 0 || $finfopic['status'] == 0) {
        //     unlink($finfoicon['filepath']);
        //     unlink($finfopic['filepath']);
        // }
        // //视频图标上传
        // if ($finfoicon['status'] == 0) {
        //     if ($finfoicon['msg'] == 'nofile') {
        //         $this->error('视频图标未上传');
        //     }
        //     $this->error($finfoicon['msg']);
        // } elseif ($finfoicon['status'] == 1) {
        //     $_POST['video_icon'] = $finfoicon['filepath'];
        // }
        //视频封面图上传
        if ($finfopic['status'] == 0) {
            if ($finfopic['msg'] == 'nofile') {
                $this->error('视频封面图未上传');
            }
            $this->error($finfopic['msg']);
        } elseif ($finfopic['status'] == 1) {
            $_POST['video_pic'] = $finfopic['filepath'];
        }

        $_POST['video_time'] = date('Y-m-d H:i:s',time());
        if(M('video')->add($_POST)){
            $this->success('添加视频成功','index');
        }else{
            $this->error('添加视频失败');
        }

    }

    //视频详情
    public function video_details(){
        $data['video_id'] = I('get.video_id');
        $video_arr = M('video')->where($data)->find();
        $this->assign('video_arr',$video_arr);
        $this->display();
    }

    //删除视频
    public function video_del(){
        $data['video_id'] = I('get.video_id');
        $data['video_display'] = 0;
        M('video')->save($data);
        $this->redirect('index');
    }

    //修改视频
    public function video_edit(){
        if (I('post.action') == 'video_edit_action') {
            $this->video_edit_action();
            exit();
        }
        $data['video_id'] = I('get.video_id');
        $video_arr = M('video')->where($data)->find();
        $this->assign('video_arr',$video_arr);
        $this->display();
    }

    //修改视频操作
    public function video_edit_action(){

        // if($_POST != array_filter($_POST)){
        //     $this->error('视频选项不能有空值！');
        // }
        //文件上传
        // $finfoicon = upload($_FILES['video_icon']);
        $finfopic = upload($_FILES['video_pic']);
        // //视频图标上传
        // if ($finfoicon['status'] == 0 && $finfoicon['msg'] != 'nofile') {
        //     $this->error($finfoicon['msg']);
        // } elseif ($finfoicon['status'] == 1) {
        //     $_POST['video_icon'] = $finfoicon['filepath'];
        // }
        //视频封面图上传
        if ($finfopic['status'] == 0 && $finfopic['msg'] != 'nofile') {
            $this->error($finfopic['msg']);
        } elseif ($finfopic['status'] == 1) {
            $_POST['video_pic'] = $finfopic['filepath'];
        }

        if(M('video')->save($_POST)){
            $this->success('修改视频成功','index');
        }else{
            $this->error('修改视频失败');
        }
    }



}